package com.Yourweibo.view;

 
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
 
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.Yourweibo.R;

public class SildeSwtichBar extends FrameLayout {
	private Context context;
	private int [] titlesImage={R.drawable.home_button,R.drawable.message_button,R.drawable.person,R.drawable.playground,R.drawable.more};
	private String [] titlesSting={"主页","消息","自己","广场","更多"};
	private TextView slideButton;
	private int oldPosition=0;
	private int currentPosition=0;
	private ImageView[] Buttons;
	private int [] background={com.Yourweibo.R.drawable.tab_select,Color.TRANSPARENT};
	private LinearLayout bodyLayout;
	private OnClickListener listner=new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
	//	v.setBackgroundResource(background[0]);

	    oldPosition=currentPosition;
	 	currentPosition=(Integer)v.getTag();
	    slideButton.setText(titlesSting[currentPosition]+30);
	    //恢复上一个tab单击事件
	    
	    Buttons[oldPosition].setClickable(true);
		
		//当前tab不可点击
		v.setClickable(false);
 
	    doAnimation();
	    slideButton.layout(currentPosition*Buttons[currentPosition].getWidth(),0,500, 50);
	    slideButton.invalidate();
	 	slideButton.setOnTouchListener(touch_linter);
 
	//	TranslateAnimation animation=new TranslateAnimation();
		}	
	};
	private OnTouchListener touch_linter=new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			System.out.println("Touch me!!!!!!!!!");
			int x=(int) event.getRawX();
			switch(event.getAction()){
			
			case MotionEvent.ACTION_DOWN:
			    System.out.println("action->Down")	;
			   
			    v.postInvalidate();  
				break;
			case MotionEvent.ACTION_MOVE:
			    System.out.println("action->monv")	;
		
			    v.postInvalidate();  
				break;
			
			
			} 
			
			
			
			return false;
		}

	 
		
		
		
	};
	public void doAnimation(){
     
	//	slideButton.layout(currentPosition*tv[currentPosition].getWidth(),slideButton.getTop(),currentPosition*tv[currentPosition].getWidth()+tv[currentPosition].getWidth(), slideButton.getBottom());
		
		TranslateAnimation MoveAnin=new TranslateAnimation((float)oldPosition*Buttons[oldPosition].getWidth(),(float)currentPosition*Buttons[currentPosition].getWidth(),0.0f, 0.0f);
 
 
 	 	MoveAnin.setDuration(500);
     	MoveAnin.setFillAfter(true);    
 
	    System.out.println("OldPosition is "+oldPosition*Buttons[oldPosition].getWidth());
	    System.out.println(" CurrentPosition is "+ currentPosition*Buttons[currentPosition].getWidth());
	 	slideButton.startAnimation(MoveAnin);
	
	//	MoveAnin.setDuration(1000);
	}
	 
	public SildeSwtichBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public SildeSwtichBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public SildeSwtichBar(Context context) {

		super(context);
	
		// TODO Auto-generated constructor stub
	}
	public void init(){
		context=this.getContext();
		//init backgroud
	
		this.setBackgroundColor(0xffffff);
	 	this.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.tab_background));
	 	//init slidButton
	 	slideButton=new TextView(context);
	   	slideButton.setBackgroundResource(background[0]);
	   	slideButton.setTextColor(Color.TRANSPARENT);
	 	slideButton.setOnTouchListener(touch_linter);
	 	  slideButton.setText(titlesSting[currentPosition]+30);
	}

	 
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		Toast.makeText(context, "OnSizeChanged",Toast.LENGTH_SHORT).show(); 
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
		
		bodyLayout=new LinearLayout(context);
		LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);  
		params.weight=1;
		Buttons=new ImageView[titlesImage.length];
		for(int i=0;i<titlesImage.length;i++){
			Buttons[i]=new ImageView(context);
			Buttons[i].setImageDrawable(context.getResources().getDrawable(titlesImage[i]));
			Buttons[i].setTag(i);
 
			Buttons[i].setOnClickListener(listner);
 
	 
		    
		    bodyLayout.addView(Buttons[i],params);
		}
	   
		FrameLayout.LayoutParams ButtonParam=new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		this.addView(slideButton,ButtonParam);
	    this.addView(bodyLayout);

		System.out.println("Inflate Finish!");
	}
	
     
}
